<div class="modal-body">
   <form id="changePriceForm" role="form" class="form-horizontal parsley-form" data-parsley-validate
              action="${base}/store/order/main/changePriceDo" method="post">
            <div class=panel-body>
                <div class="table-responsive no-border">
                    <input type="hidden" name="id" value="${obj.id}">
                    <table class="table table-bordered saleOffDatatable">
                        <caption>订单原价: ¥<span id="oldPayMoney">${@money.fenToYuan(obj.payMoney)} 元 </span></caption>
                        <thead>
                        <tr>
                            <th>购买商品</th>
                            <th>原价</th>
                            <th>修改后价格</th>
                            <th>数量</th>
                            <th>购买价</th>
                        </tr>
                        <!--# for(o in obj.goodsList){ #-->
                        <tr class="priceTable" lang="${o.id}">
                            <td>${o.name} <!--# if(isNotEmpty(o.spec)){
                                                    var specGroup = @org.nutz.json.Json.fromJson(o.spec);
                                                    for (g in specGroup) {
                                                        print(g.spec_name + ":" + g.spec_value_name);
                                                        if (!gLP.last) print(" * ");
                                                    }
                                                 }
                                           #--></td>
                            <td>${@money.fenToYuan(o.salePrice)}</td>
                            <td>
                                <div class="input-group">
                                    <span class="input-group-addon">￥</span>
                                    <input  type="text" class='form-control priceItem' data-parsley-price="true" style="width: 120px" value=${@money.fenToYuan(o.buyPrice)}>
                                </div>
                            </td>
                            <td class="buyNum">${o.buyNum}</td>
                            <td class="totalMoney">${@money.fenToYuan(o.totalMoney)}</td>
                        </tr>
                        <!--# }#-->
                        <tr>
                            <td colspan="5">
                                <div class="price">
                                    <label class="col-md-2 control-label">运费：</label>
                                    <div class="input-group">
                                        <span class="input-group-addon">￥</span>
                                        <input type='text' name="freightMoney" class='form-control' style="width: 120px" data-parsley-price="true" value=${@money.fenToYuan(obj.freightMoney)}>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        </thead>
                    </table>
                </div>
                <div class="well well-lg">
                    <span style="color: #2a3037">
                    买家：${accountUser.accountInfo.nickname}<br>
                    收货地址：${@area.getNameByCode(obj.deliveryProvince)} ${@area.getNameByCode(obj.deliveryCity)} ${@area.getNameByCode(obj.deliveryCounty)} ${obj.deliveryAddress}<br>
                    买家实付： 商品总价(<span id="goodsPayMoney" class="price"> ${@money.fenToYuan(obj.goodsPayMoney)}</span>) + 运费(<span class="freightMoney price" >${@money.fenToYuan(obj.freightMoney)}</span>) - 促销优惠：<span id="freeMoney" class="price">${@money.fenToYuan(obj.freeMoney)}</span>	= <span class="payMoney price" >${@money.fenToYuan(obj.payMoney)}</span> 元<br></span>
                    <span>买家实付：= 商品修改后价格 + 运费 - 促销优惠</span><br>
                </div>
            </div>
        </form>
   <div id="dialogAffirm" class="modal fade bs-modal-sm" tabindex="-2" role="dialog" aria-hidden="true">
        <div class="modal-dialog" style="top:30%">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" onclick="priceClose()" aria-hidden="true">×</button>
                    <h4 class="modal-title">确认改价</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-xs-6">
                            <label>商品原金额:<span class="price oldGoodsPrice"></span></label><br>
                            <label>运费原金额: <span class="price oldFreightMoney"></span></label><br>
                            <label>订单原总金额: <span class="price oldPayMoney"></span></label>
                        </div>
                        <div class="col-xs-6">
                            <label>商品改价后金额: <span class="price goodsPrice"></span></label><br>
                            <label>运费改价后金额: <span class="price freightMoney"></span></label><br>
                            <label>订单改价后金额: <span class="price payMoney"></span></label>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" style="float: left" onclick="priceClose()">关闭</button>
                    <button type="button" class="btn btn-primary"  data-dismiss="modal"  onclick="save()" data-loading-text="${msg['globals.button.delete.tip']}">${msg['globals.button.confirm']}</button>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" style="float: left" data-dismiss="modal" >关闭</button>
    <button class="btn btn-primary navbar-btn" type="button" id="save" >确认</button>
</div>
<style>
    .price{
        color: #eb1409;
    }
</style>
<script>
    //订单号
    var id = $("input[name='id']").val();
    /*商品改价*/
    var oldGoodsPrice = parseFloat($("#goodsPayMoney").text());
    var goodsPrice = oldGoodsPrice;
    $(".priceItem").on("input propertychange",function () {
        goodsPrice = 0;
        if($(this).val() < 0 || $(this).val() == ""){
            $(this).val(0)
        }else{
            //$(this).val(parseFloat($(this).val()));
        }
        $(".priceTable").each(function () {
            var totalMoney;
            var buyNum = parseInt($(this).children(".buyNum").text());
            totalMoney = FloatMul(parseFloat($(this).find(".priceItem").val()),buyNum).toFixed(2);
            goodsPrice  = FloatAdd(goodsPrice,totalMoney) ;
            $(this).find(".totalMoney").text(totalMoney);
        });

        $("#goodsPayMoney").text(goodsPrice.toFixed(2));
        //更新应付金额
        countPayMoney();
    });

    /*运费*/
    var oldFreightMoney = parseFloat($("input[name='freightMoney']").val());
    var freightMoney= oldFreightMoney;
    $("input[name='freightMoney']").on("input propertychange",function () {
        if($(this).val() < 0 || $(this).val() == ""){
            $(this).val(0)
        }else{
            //$(this).val(parseFloat($(this).val()));
        }
        freightMoney = parseFloat($(this).val());
        $(".well").find(".freightMoney").text(freightMoney.toFixed(2));
        //更新应付金额
        countPayMoney();
    })

    /*统计订单应付总金额*/
    var oldPayMoney= parseFloat($(".well").find(".payMoney").text());
    var payMoney= oldPayMoney;
    var freeMoney=parseFloat($('#freeMoney').text());//优惠金额现在默认
    function countPayMoney() {
        payMoney = FloatSub(FloatAdd(goodsPrice,freightMoney),freeMoney);
        $(".well").find(".payMoney").text(payMoney);
    }

    $('#save').click(function () {
        $("#dialogAffirm").modal("show");
        $(".row").find(".oldGoodsPrice").text(oldGoodsPrice.toFixed(2));
        $(".row").find(".oldFreightMoney").text(oldFreightMoney.toFixed(2));
        $(".row").find(".oldPayMoney").text(oldPayMoney.toFixed(2));
        $(".row").find(".goodsPrice").text(goodsPrice.toFixed(2));
        $(".row").find(".freightMoney").text(freightMoney.toFixed(2));
        $(".row").find(".payMoney").text(payMoney);
    })

    var needTransfer = ["freightMoney","goodsPayMoney","payMoney"];

    $(document).ready(function () {
        $('#changePriceForm').ajaxForm({
            dataType: 'json',
            beforeSubmit: function (arr, form, options) {
                var goodsPayMoney = {
                    name: "goodsPayMoney",
                    required: false,
                    value: goodsPrice,
                }
                var payPrice={
                    name: "payMoney",
                    required: false,
                    value: payMoney,
                }
                var goodsList=[];
                $(".priceTable").each(function () {
                    var goods ={};
                    goods.id = $(this).attr("lang");
                    goods.buyPrice = FloatMul(parseFloat($(this).find(".priceItem").val()),100);
                    goods.totalMoney = FloatMul(parseFloat($(this).find(".totalMoney").text()),100);
                    goodsList.push(goods);
                })
                var goods = {
                    name: "goods",
                    required: false,
                    value: JSON.stringify(goodsList)
                }
                arr.push(goodsPayMoney);
                arr.push(payPrice);
                arr.push(goods);
                for(var i in arr){
                    //若提交的表单包含以上的金额,则元转换为分
                    if(needTransfer.contains(arr[i].name)){
                        var val =  arr[i].value;
                        arr[i].value = val * 100;
                    }
                }
                form.find("button:submit").button("loading");
            },
            success: function (data, statusText, xhr, form) {
                if (data.code == 0) {
                    Toast.success(data.msg);
                    $("#oldPayMoney").text(payMoney);
                    $("#dialogAffirm").modal("hide");
                    setTimeout(function () {
                        window.location.reload();
                    },500)
                } else {
                    $("#dialogAffirm").modal("hide");
                    Toast.error(data.msg);
                }
                form.find("button:submit").button("reset");
            }
        });
    });

    function save() {
        $('#changePriceForm').submit();
    }
    
    /*$("#save").on("click",function () {
        $('#changePriceForm').submit();
    })*/

    function accMul(arg1,arg2)
    {
        var m=0,s1=arg1.toString(),s2=arg2.toString();
        try{m+=s1.split(".")[1].length}catch(e){}
        try{m+=s2.split(".")[1].length}catch(e){}
        return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
    }

    function priceClose() {
        $("#dialogAffirm").modal("hide");
    }

    $('#dialogAffirm').on('hidden.bs.modal', function() {
        $('#dialogOperation').css({'overflow-y':'scroll'});
    });

    //浮点数加法运算
    function FloatAdd(arg1,arg2){
        var r1,r2,m;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2));
        return (arg1*m+arg2*m)/m;
    }

    //浮点数减法运算
    function FloatSub(arg1,arg2){
        var r1,r2,m,n;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2));
        //动态控制精度长度
        n=(r1=r2)?r1:r2;
        return ((arg1*m-arg2*m)/m).toFixed(2);
    }

    //浮点数乘法运算
    function FloatMul(arg1,arg2)
    {
        var m=0,s1=arg1.toString(),s2=arg2.toString();
        try{m+=s1.split(".")[1].length}catch(e){}
        try{m+=s2.split(".")[1].length}catch(e){}
        return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
    }

    //数组的包含方法
    Array.prototype.contains = function ( needle ) {
        for (i in this) {
            if (this[i] == needle) return true;
        }
        return false;
    }

</script>